package com.cloudkinto.controller.mail;

import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.mail.SendMailService;
import com.cloudkinto.service.mail.vo.SendMailReq;
import com.cloudkinto.service.mail.vo.testAutoDo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Api(tags = "发送邮箱模块")
@RequestMapping("/api/admin/oms/sendMail")
public class SendMailController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(MailController.class);

    @Autowired
    private SendMailService service;

    @UserSessionRequired
    @PostMapping()
    @ApiOperation(value = "向用户发邮件")
    public SingleResult sendEmail(@RequestBody SendMailReq req) throws Exception {
            return service.sendEmail(req);
    }

    //预览邮件
    @UserSessionRequired
    @PostMapping("/preview")
    @ApiOperation(value = "预览邮件")
    public SingleResult preview(@RequestBody SendMailReq req) throws Exception {
        return service.preview(req);
    }
    @UserSessionRequired
    @PostMapping("/testAuto")
    @ApiOperation(value = "测试自动")
    public SingleResult testAuto(@RequestBody testAutoDo req) throws Exception {
        List<Long> ids = req.getIds();
        Long storeId = req.getStoreId();
        int mailType = req.getMailType();
        return service.autoSendEmail(mailType,storeId,ids);
    }

    //定时任务清除数据库数据
    @Scheduled(cron = "0 2 17 * * ?")
    public void scheduleDeleteOldData() {
        service.deleteRecordsOlderThan7Days();
    }



}
